package pwd.allen.web.config;

import com.alibaba.druid.support.spring.stat.DruidStatInterceptor;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.JdkRegexpMethodPointcut;
import org.springframework.context.annotation.*;
import pwd.allen.config.AOPConfig;

/**
 * 容器配置的方式：
 * 	1）基于web.xml
 * 	2）基于java配置类：继承 {@link org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer} 设置根容器和子容器的配置类、servletMappings等
 * 	3）基于xml配置：继承 {@link org.springframework.web.servlet.support.AbstractDispatcherServletInitializer}
 *
 * @author 门那粒沙
 * @create 2021-05-01 9:24
 **/
@Configuration
@ComponentScan(basePackages="pwd.allen.web")
@Import({AOPConfig.class, RequestLimitConfig.class})//把AOP配置放到spring mvc容器里，不然自定义的AOP在controller层不起效（因为不在pwd.allen.web路径下扫描不到）
public class WebConfig {

    //<editor-fold desc="druid的spring监控为空">
    @Bean
    public DruidStatInterceptor druidStatInterceptor() {
        DruidStatInterceptor dsInterceptor = new DruidStatInterceptor();
        return dsInterceptor;
    }
    @Bean
    @Scope("prototype")
    public JdkRegexpMethodPointcut druidStatPointcut() {
        JdkRegexpMethodPointcut pointcut = new JdkRegexpMethodPointcut();
        // Spring监控AOP切入点，如x.y.z.service.*,配置多个英文逗号分隔
        // 问题：如果设置成dao会没有信息，猜想是因为处在不同的spring容器中
        pointcut.setPattern("pwd.allen.web.controller.*");
        return pointcut;
    }
    @Bean
    public DefaultPointcutAdvisor druidStatAdvisor(DruidStatInterceptor druidStatInterceptor, JdkRegexpMethodPointcut druidStatPointcut) {
        DefaultPointcutAdvisor defaultPointAdvisor = new DefaultPointcutAdvisor();
        defaultPointAdvisor.setPointcut(druidStatPointcut);
        defaultPointAdvisor.setAdvice(druidStatInterceptor);
        return defaultPointAdvisor;
    }
    //</editor-fold>
}
